package com.mymall.serve.controller;

import com.mymall.serve.common.RootResult;
import com.mymall.serve.interceptor.RequestThreadLocal;
import com.mymall.serve.multicaster.event.UpdateRankListEvent;
import com.mymall.serve.pojo.bo.LoginUserBO;
import com.mymall.serve.pojo.vo.ProductVO;
import com.mymall.serve.serviceimpl.ProductServiceImpl;
import com.mymall.serve.utils.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 产品controller类
 *
 * @author weiwei
 * @since 2020-04-12 11:45
 **/
@Validated
@Slf4j
@RestController
@RequestMapping(value = "/product")
public class ProductController {

    @Autowired
    private ProductServiceImpl productService;

    private static final String rankKeyName = "local_product_hits";

    /**
     * （测试-访问）通过商品id获取商品信息
     *
     * @author weiwei
     * @param productId 商品id
     * @date 2020/4/12 17:44
     * @return com.mymall.serve.common.RootResult<com.mymall.serve.pojo.vo.ProductVO>
     */
    @GetMapping("/getProductById")
    public RootResult<ProductVO> getProductByIdTest(@NotNull(message = "id不能为空") Long productId,HttpServletRequest request){
        // 测试使用requestThreadLocal
//        LoginUserBO loginUserBO = RequestThreadLocal.getLoginUser();
//        if (loginUserBO != null) {
//            log.info("用户{}，请求{}", loginUserBO.getUsername(), request.getRequestURI());
//        }
        ProductVO productByIdTest = productService.getProductByIdTest(productId);
        if (productByIdTest != null) {
            UpdateRankListEvent asyncEvent = new UpdateRankListEvent(this);
            asyncEvent.setKey(rankKeyName);
            asyncEvent.setValue(String.valueOf(productId));
            asyncEvent.setScore(1.0D);
            SpringUtil.publishApplicationEvent(asyncEvent);
        }

        return RootResult.success(productByIdTest);
    }

    /**
     * （测试-访问）通过商品点击排行榜
     *
     * @author weiwei
     * @param
     * @date 2020/4/16 15:39
     * @return com.mymall.serve.common.RootResult<java.util.List<com.mymall.serve.pojo.vo.ProductVO>>
     */
    @GetMapping("/getProductRankTop10")
    public RootResult<List<ProductVO>> getProductHitList(){
        return RootResult.success(productService.getProductHitList(rankKeyName));
    }

    /**
     * （测试-访问）通过商品id获取商品信息
     *
     * @author weiwei
     * @date 2020/4/12 17:44
     * @return com.mymall.serve.common.RootResult<com.mymall.serve.pojo.vo.ProductVO>
     */
    @GetMapping("/getProductList")
    public RootResult<List<ProductVO>> getProductList(HttpServletRequest request){
        // 测试使用requestThreadLocal
//        LoginUserBO loginUserBO = RequestThreadLocal.getLoginUser();
        //log.info("用户{}，请求{}",loginUser.getUsername(),request.getRequestURI());
        return RootResult.success(productService.getProductList());
    }
}
